package com.strato.logging.v1_0_0.api;

import java.util.Collection;

import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;

import com.strato.logging.v1_0_0.activator.Activator;
import com.strato.logging.v1_0_0.log4j.Log4jLogFactory;


public interface LogFactory {
	
	public Log getLogByName(String name);
	
	public static LogFactory getSystem(){
		return Log4jLogFactory.getInstance();
	}
	public static LogFactory getDefault(){
		try {
			BundleContext bc = Activator.getContext();
			if(bc==null){
				return getSystem();
			}
			Collection<ServiceReference<LogFactory>> srs = bc.getServiceReferences(LogFactory.class,null);
			if(srs!=null&&!srs.isEmpty()){
				ServiceReference<LogFactory> sr = srs.iterator().next();
				LogFactory lf = bc.getService(sr);
				//TODO cache
				return lf;
			}
		} catch (InvalidSyntaxException e) {
			e.printStackTrace();
		}
		return getSystem();
	}
	
	public static Log getLog(String name){
		return getDefault().getLogByName(name);
	}

	public static Log getLog(Class<?> clazz){
		return getLog(clazz.getName());
	}

	public static Log getSystemLog(String name){
		return getSystem().getLogByName(name);
	}

	public static Log getSystemLog(Class<?> clazz){
		return getSystemLog(clazz.getName());
	}
}
